#ifndef SINGLETICKETWIDGET_H
#define SINGLETICKETWIDGET_H

#include <QtGui/QWidget>
#include <QtGui/QPushButton>
#include <QtGui/QStyledItemDelegate>

namespace Ui {
    class SingleTicketWidget;
}

class Ticket;
class TicketChangeset;
class TicketExplorer;
class TicketAction;
class InformationWidget;
class TicketError;

class ChangeLogModel : public QAbstractListModel
{
    Q_OBJECT
public:
    enum CsRoles {
        AuthorRole = Qt::UserRole + 255,
        TimestampRole,
        ChangedFieldsRole,
        CommentRole
    };

    ChangeLogModel(QObject *parent = 0)
        : QAbstractListModel(parent)
    {
    }

    QVariant data(const QModelIndex &index, int role) const;
    int rowCount(const QModelIndex &parent) const;

    void setEntries(QList<TicketChangeset *> newEntries);
    void clear();
private:
    QList<TicketChangeset *> m_entries;
};

class ChangeLogDelegate : public QStyledItemDelegate
{
public:
    void paint(QPainter *painter, const QStyleOptionViewItem &option,
                const QModelIndex &index ) const;
    QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
};

class SingleTicketWidget : public QWidget
{
    Q_OBJECT

public:
    enum FormMode {
        NewTicket,
        EditTicket
    };

    SingleTicketWidget(Ticket *ticket, TicketExplorer *explorer, QWidget *parent = 0);
    explicit SingleTicketWidget(TicketExplorer *explorer, QWidget *parent = 0);

    ~SingleTicketWidget();
    void loadTicket(Ticket *ticket);
    void setFormMode(const FormMode &formMode);

protected:
    void closeEvent(QCloseEvent *event);

private slots:
    void actionSelected(const TicketAction *action, const QString &inputValue);
    void ticketUpdated();
    void ticketCreated();
    void historyLoaded();
    void actionsLoaded();
    void recentlyChangedTicketsListed(const QList<int> &tickets);
    void periodicalUpdate();
    void onError(const TicketError &error);
    void reload();
    void lockForm(bool lock);
    void unlockForm();
    void enableMainEdit(bool enable);
    void enableDetailsEdit(bool enable);

    void on_detailsButtonEditMain_clicked();
    void on_detailsButtonEditActions_clicked();
    void on_buttonAddNewComment_clicked();
    void on_buttonCancel_clicked();
    void buttonReply_clicked();
    void on_buttonApplyComment_clicked();
    void on_buttonSaveMainChanges_clicked();
    void on_buttonSaveActionChanges_clicked();
    void on_buttonCreateTicket_clicked();

private:
    void setupForm();
    void updateForm();

    Ui::SingleTicketWidget *ui;
    TicketExplorer *m_te;
    Ticket *m_ticket;
    const TicketAction *m_currentAction;
    bool m_reloadHistory;

    QTimer *m_periodicalUpdateTimer;
    InformationWidget *m_infoWidget;
    QWidget *m_actionFrame;
    QString m_actionValue;
};

class QRadioButton;
class QComboBox;

class ActionWidget : public QWidget
{
    Q_OBJECT
public:
    ActionWidget(const TicketAction *action, QButtonGroup *group, QWidget *parent = 0);

    const TicketAction *action() const;
    QString inputValue() const;
    void toggle();

private slots:
    void radioButtonToggled(bool value);
    void inputValueChanged();

signals:
    void selected(const TicketAction *action, const QString &inputValue);

private:
    void enableDisableWidgets(bool enable);

    const TicketAction *m_action;
    QRadioButton *m_radio;
    QComboBox *m_fields;
    QLineEdit *m_value;
};

#endif // SINGLETICKETWIDGET_H
